//
//  PhotoBrowserVC.m
//  Borderless
//
//  Created by Bager on 2017/10/12.
//  Copyright © 2017年 Bager. All rights reserved.
//

#import "PhotoBrowserVC.h"

#import "ZLPhotoActionSheet.h"
#import "ZLDefine.h"
#import "ImageCell.h"
#import "ZLPhotoModel.h"

@interface PhotoBrowserVC ()

@property (nonatomic, strong) NSMutableArray<UIImage *> *lastSelectPhotos;
@property (nonatomic, strong) NSMutableArray<PHAsset *> *lastSelectAssets;
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) NSArray *arrDataSources;

@end

@implementation PhotoBrowserVC

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
    [self.navigationController setNavigationBarHidden:YES animated:animated];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:YES];
    [self.navigationController setNavigationBarHidden:NO animated:animated];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    self.view.backgroundColor = [UIColor whiteColor];

    [self initCollectionView];
    
    [self btnSelectPhotoLibrary];
}

- (void)initCollectionView
{
    CGFloat width = [UIScreen mainScreen].bounds.size.width;
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake((width-9)/4, (width-9)/4);
    layout.minimumInteritemSpacing = 1.5;
    layout.minimumLineSpacing = 1.5;
    layout.sectionInset = UIEdgeInsetsMake(3, 0, 3, 0);
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 64, kScreenWidth, kScreenHeight - 64) collectionViewLayout:layout];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    [self.collectionView registerClass:NSClassFromString(@"ImageCell") forCellWithReuseIdentifier:@"ImageCell"];
    [self.view addSubview:self.collectionView];
}

- (ZLPhotoActionSheet *)getPas
{
    ZLPhotoActionSheet *actionSheet = [[ZLPhotoActionSheet alloc] init];
    
#pragma optional
    //以下参数为自定义参数，均可不设置，有默认值
//    weakify(self);
//    actionSheet.cancelBlock = ^{
//        [weakSelf.navigationController popViewControllerAnimated:YES];
//    };
//    actionSheet.navBarColor = kPublicColorMain;
//    actionSheet.sortAscending = YES;
//    actionSheet.allowSelectImage = YES;
//    actionSheet.allowSelectGif = NO;
//    actionSheet.allowSelectVideo = NO;
//    actionSheet.allowSelectLivePhoto = NO;
//    actionSheet.allowForceTouch = NO;
//    actionSheet.allowEditImage = YES;
//    actionSheet.allowSlideSelect = YES;
//    actionSheet.allowMixSelect = YES;
//    //设置相册内部显示拍照按钮
//    actionSheet.allowTakePhotoInLibrary = YES;
//    //设置在内部拍照按钮上实时显示相机俘获画面
//    actionSheet.showCaptureImageOnTakePhotoBtn = YES;
//    //设置照片最大预览数
//    actionSheet.maxPreviewCount = 20;
//    //设置照片最大选择数
//    actionSheet.maxSelectCount = 9;
//    //设置允许选择的视频最大时长
//    actionSheet.maxVideoDuration = 60 * 60;
//    //设置照片cell弧度
//    actionSheet.cellCornerRadio = 0;
//    //单选模式是否显示选择按钮
//    actionSheet.showSelectBtn = NO;
//    //是否在选择图片后直接进入编辑界面
//    actionSheet.editAfterSelectThumbnailImage = NO;
//    //设置编辑比例
//    //    actionSheet.clipRatios = @[GetClipRatio(4, 3)];
//    //是否在已选择照片上显示遮罩层
//    actionSheet.showSelectedMask = YES;
    //遮罩层颜色
    //    actionSheet.selectedMaskColor = [UIColor orangeColor];
#pragma required
    //如果调用的方法没有传sender，则该属性必须提前赋值
    actionSheet.sender = self;
    
//    actionSheet.arrSelectedAssets = nil;
    
    [actionSheet setSelectImageBlock:^(NSArray<UIImage *> * _Nonnull images, NSArray<PHAsset *> * _Nonnull assets, BOOL isOriginal) {
//        strongify(weakSelf);
//        strongSelf.arrDataSources = images;
//        strongSelf.lastSelectAssets = assets.mutableCopy;
//        strongSelf.lastSelectPhotos = images.mutableCopy;
//        [strongSelf.collectionView reloadData];
//
//        weakSelf.selectImageBlock(images, assets, isOriginal);
        
//        [weakSelf.navigationController popViewControllerAnimated:YES];
    }];
    
    return actionSheet;
}

- (void)btnSelectPhotoPreview
{
    [self showWithPreview:YES];
}

- (void)btnSelectPhotoLibrary
{
    [self showWithPreview:NO];
}

- (void)showWithPreview:(BOOL)preview
{
    ZLPhotoActionSheet *a = [self getPas];
    
    if (preview) {
        [a showPreviewAnimated:YES];
    } else {
        [a showPhotoLibrary];
    }
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _arrDataSources.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ImageCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ImageCell" forIndexPath:indexPath];
    cell.imageView.image = _arrDataSources[indexPath.row];
    PHAsset *asset = self.lastSelectAssets[indexPath.row];
    cell.playImageView.hidden = !(asset.mediaType == PHAssetMediaTypeVideo);
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
//    [[self getPas] previewSelectedPhotos:self.lastSelectPhotos assets:self.lastSelectAssets index:indexPath.row];
}


@end

